package blog.gui;

import java.io.IOException;
/* Start Modify 08-10-20 */
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;
/* End Modify 08-10-20 */
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import blog.ConstString;
import blog.SysRuntime;
import blog.controller.ConstGui;
import blog.db.BlogAccount;
import blog.db.BlogAccountDB;

public class BlogAccountManagerGui extends List implements IBaseGui, CommandListener {
	private BlogAccount[] data;
	
	private static Command CMD_REGIST_USER = new Command(ConstString.CMD_REGIST_USER, Command.SCREEN, 2);
	private static Command CMD_COMFIRM = new Command(ConstString.CMD_SELECT, Command.OK, 2);
	private static Command CMD_ADD = new Command(ConstString.CMD_ADD, Command.SCREEN, 4);
	private static Command CMD_UPDATE = new Command(ConstString.CMD_EDIT, Command.SCREEN, 3);
	private static Command CMD_DELETE = new Command(ConstString.CMD_REMOVE, Command.SCREEN, 5);
	private static Command CMD_EXIT = new Command(ConstString.CMD_EXIT, Command.EXIT, 0);
	private static Command CMD_ABOUT = new Command(ConstString.CMD_ABOUT, Command.HELP, 0);
	private static Command CMD_BACK = new Command( ConstString.CMD_BACK, Command.BACK, 0);
	/* Start Modify 08-10-20 */
	private static Command CMD_VISIT = new Command(ConstString.CMD_VISIT, Command.OK, 0);
	/* End Modify 08-10-20 */
	
	private static BlogAccountManagerGui _instance;
	
	private Image accountImage;
	
	public synchronized static BlogAccountManagerGui getInstance()
	{
		if ( _instance==null )
		{
			_instance = new BlogAccountManagerGui();
		}
		return _instance;
	}

	private BlogAccountManagerGui() {
		super( ConstString.GUI_TITLE_BLOG_ACCOUNT_MANAGER, List.IMPLICIT );
		try {
			accountImage = Image.createImage("/account.png");
		} catch (IOException e) {
		}
		setFitPolicy(List.TEXT_WRAP_ON);
		this.addCommand( CMD_ADD );
		this.addCommand( CMD_EXIT );
		this.addCommand(CMD_ABOUT);
		this.setCommandListener( this );
		
		reloadData();		
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_BLOG_ACCOUNT_MANAGER;
	}

	public void notifyChanged(Object param) {
		
	}

	public void reloadData()
	{
		data = BlogAccountDB.getAllIndex();
		this.deleteAll();
		if ( data.length>0 )
		{
			for( int i=0; i<data.length; i++ )
			{
				if ( data[i].isChecked( ) )
				{
					this.append(data[i].getAccountName(), accountImage);
				}
				else
				{
					this.append( data[i].getAccountName()+ConstString.GUI_BLOG_ACCOUNT_UNCHECK, accountImage );
				}
			}
		}
		else
		{
			this.append(ConstString.GUI_BLOG_ACCOUNT_MANAGER_NO_DATA, null);
		}
		this.setSelectedIndex(0, true);
		
		resetCmd();
	}
	
	private void resetCmd()
	{
		if ( data.length==0 )
		{
			this.removeCommand( CMD_COMFIRM );
			this.removeCommand( CMD_DELETE );
			this.removeCommand( CMD_UPDATE );
		}
		else
		{
			this.addCommand( CMD_COMFIRM );
			this.addCommand( CMD_DELETE );
			this.addCommand( CMD_UPDATE );
		}
		
		if ( !SysRuntime.registedUser )
		{
			this.addCommand( BlogAccountManagerGui.CMD_REGIST_USER );
		}
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==CMD_ADD )
		{
			BlogAccountGui nextScreen = BlogAccountGui.getInstance();
			nextScreen.setData(null);
			nextScreen.setOperationType(BlogAccountGui.OP_TYPE_ADD);
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==List.SELECT_COMMAND || cmd==CMD_COMFIRM )
		{
			BlogAccount tmpAcc = data[this.getSelectedIndex()];
			if ( tmpAcc.isChecked( ) )
			{
				BlogAccount selectedAccount = BlogAccountDB.get( tmpAcc.getAccountName( ) );
				if ( selectedAccount!=null )
				{
					SysRuntime.curAccount = selectedAccount;
					BlogMainViewGui.getInstance().reloadData();
					SysRuntime.guiManager.show( BlogMainViewGui.getInstance() );
				}
			}
			else
			{
				Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.GUI_ACCOUNT_UNCHECK_PROMPT, null, AlertType.WARNING);
				alert.setTimeout( 5000 );
				SysRuntime.guiManager.showAlert( alert, true );
			}
		}
		else if ( cmd==CMD_DELETE )
		{
			String accountName = data[this.getSelectedIndex()].getAccountName();
			BlogAccountDB.delete(accountName);
			this.reloadData();
		}
		else if ( cmd==CMD_EXIT )
		{
			SysRuntime.midlet.destroyApp(true);
		}
		else if ( cmd==CMD_UPDATE )
		{
			String accountName = data[this.getSelectedIndex()].getAccountName( );
			BlogAccount account = BlogAccountDB.get(accountName);
			BlogAccountGui nextScreen = BlogAccountGui.getInstance();
			nextScreen.setData(account);
			nextScreen.setOperationType(BlogAccountGui.OP_TYPE_EDIT);
			SysRuntime.guiManager.show(nextScreen);
		}
		else if ( cmd==CMD_ABOUT )
		{
			Form about = new Form(ConstString.GUI_TITLE_ABOUT);
			/* Start Modify 08-10-20 */
			try
			{
				about.append( new ImageItem("", Image.createImage( "/about.png" ), ImageItem.LAYOUT_CENTER|ImageItem.LAYOUT_NEWLINE_AFTER, "") );
			}
			catch (IOException e)
			{
				e.printStackTrace( );
			}
			StringItem tmp = new StringItem("", ConstString.ABOUT_PIC_BLOW, StringItem.PLAIN);
			tmp.setLayout( StringItem.LAYOUT_CENTER );
			about.append( tmp );
			about.append( "\n" );
			about.addCommand(CMD_VISIT);
			/* End Modify 08-10-20 */
			about.append(ConstString.ABOUT);
			about.addCommand(CMD_BACK);
			about.setCommandListener(this);
			SysRuntime.guiManager.show(about);
		}
		else if ( cmd==CMD_BACK )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==CMD_REGIST_USER )
		{
			SysRuntime.guiManager.show( new VerifyUserGui(VerifyUserGui.TYPE_BACK) );
		}
		/* Start Modify 08-10-20 */
		else if ( cmd==CMD_VISIT )
		{
			try {
				SysRuntime.midlet.platformRequest(ConstString.URL_HOME_PAGE);
			} catch (ConnectionNotFoundException e) {
				//#debug
				e.printStackTrace();
				Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.PROMPT_VISIT_BLOG_FAILED, null, AlertType.WARNING);
				SysRuntime.guiManager.showAlert(alert, true);
			}
		}
		/* End Modify 08-10-20 */
	}
}